<?php
/**
 * Created by PhpStorm.
 * User: luo
 * Date: 2018/9/26
 * Time: 22:30
 */

namespace app\models;

use app\tools\Tool;
use Yii;
use app\tools\Time;
use app\models\User;
use app\models\UserLogin;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\helpers\Inflector;

/**
 * Class Base
 * @property $id
 * @property $user_id
 * @property $created_at
 * @property User $user
 * @package app\models
 */
class Base extends ActiveRecord
{

    const PAGE_SIZE = 20;

    /**
     * 全局统一的状态标识，无特殊情况，统一按此标准执行
     */
    const STATUS_ON  = 1;
    const STATUS_OFF = 0;
    const STATUS_DEL = -1;

    public function init()
    {
        parent::init();
        $this->on(self::EVENT_BEFORE_VALIDATE, [$this, 'onSetDefaultAttributes']);
        if($this->hasMethod('onRemoveUrlPrefix')){
            $this->on(self::EVENT_BEFORE_VALIDATE, [$this, 'onRemoveUrlPrefix']);
        }
    }

    public function onSetDefaultAttributes(){
        if(
            $this->hasAttribute('user_id') &&
            ($this->user_id === null || $this->user_id === '') &&
            Yii::$app->has('user') &&
            !Yii::$app->user->isGuest){

            $this->user_id = Yii::$app->user->id;
        }

        if($this->hasAttribute('created_at') && ($this->created_at === null) && $this->isNewRecord){
            $this->created_at = time();
        }
    }

    public function getUser(){
        /* @var $this \yii\db\ActiveRecord */
        return $this->hasOne(User::class, ['id'=>'user_id']);
    }

    public function getAdmin(){
        /* @var $this \yii\db\ActiveRecord */
        return $this->hasOne(User::class, ['id'=>'admin_id']);
    }

    public function showErrors(){
        $errors = '';
        foreach ($this->errors as $v){
            foreach ($v as $v2){
                $errors .= $v2."\n";
            }
        }
        return $errors;
    }

    /**
     * 用于自动生成创建时间的展示
     * @param $type string friendly short
     * @return string
     */
    public function showShortTime(){
        return date('Y-m-d', $this->created_at);

    }

    public function showTime(){
        return date('Y-m-d H:i', $this->created_at);
    }

    function showCreatedAt(){
        return date('Y-m-d H:i:s', $this->created_at);
    }

    /**
     * @param Base[] $model_list
     * @param string[] $fields
     * @return array
     */
    public static function buildParamsList($model_list, $fields){
        $list = [];
        foreach ($model_list as $k=>$v){
            $list[] = self::buildParam($v, $fields);
        }
        return $list;
    }

    public function buildParams($fields){
        return Base::buildParam($this, $fields);
    }

    /**
     * @param $model self
     * @param $fields
     * @return array
     */
    public static function buildParam($model, $fields){
        $data = [];
        foreach ($fields as $v) {
            $method_name = 'show' . Inflector::camelize($v);
            if (method_exists($model, $method_name)) {
                $data[$v] = $model->$method_name();
            } elseif ($model->hasAttribute($v)) {
                $data[$v] = $model->$v;
            } elseif (property_exists($model, $v)) {
                $data[$v] = $model->$v;
            } else {
                $data[$v] = '';
            }
        }
        return $data;
    }
}
